/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vistas;

import com.util.Automata;
import com.util.ExpresionRegular;
import com.util.LeerXml;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.button.StandardButtonShaper;

/**
 *
 * @author rufersic
 */
public class ViewInicio extends javax.swing.JFrame {

    private Automata a;
    

    public JTable getjTDeterminista() {
        return JDeterminista;
    }

    public void setjTDeterminista(JTable jTDeterminista) {
        this.JDeterminista = jTDeterminista;
    }

    public JTable getjTNoDeterminista() {
        return jTNoDeterminista;
    }

    public void setjTNoDeterminista(JTable jTNoDeterminista) {
        this.jTNoDeterminista = jTNoDeterminista;
    }
    
    public Automata getA() {
        return a;
    }

    public void setA(Automata a) {
        this.a = a;
    }

    /**
     * Creates new form Convertidor_Automata
     */
    public ViewInicio() {
        initComponents();
         this.jBConvertir.putClientProperty( SubstanceLookAndFeel.BUTTON_SHAPER_PROPERTY, new StandardButtonShaper());
          this.jBvalidar.putClientProperty( SubstanceLookAndFeel.BUTTON_SHAPER_PROPERTY, new StandardButtonShaper());
         jTNoDeterminista.setVisible(false);
        JDeterminista.setVisible(false);
        jTextField1.setVisible(false);
        jTextField2.setVisible(false);
        jTextField3.setVisible(false);
        jLabel1.setText("");
        jLabel2.setText("");
        jLabel3.setText("");
        jBvalidar.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTNoDeterminista = new javax.swing.JTable();
        jBConvertir = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        JDeterminista = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jBvalidar = new javax.swing.JButton();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMLerAutomata = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTNoDeterminista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Title 1"
            }
        ));
        jScrollPane3.setViewportView(jTNoDeterminista);

        jBConvertir.setText("Convertir");
        jBConvertir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBConvertirActionPerformed(evt);
            }
        });

        JDeterminista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Title 1"
            }
        ));
        jScrollPane5.setViewportView(JDeterminista);

        jLabel1.setText("Ingrese expresion validadora : ");

        jLabel2.setText("Ingrese expresion a validar :");

        jLabel3.setText("resultado:");

        jBvalidar.setText("validar expresion");
        jBvalidar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBvalidarActionPerformed(evt);
            }
        });

        jMenu3.setText("Automata");

        jMLerAutomata.setText("Leer Automata");
        jMLerAutomata.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMLerAutomataActionPerformed(evt);
            }
        });
        jMenu3.add(jMLerAutomata);

        jMenuBar2.add(jMenu3);

        jMenu4.setText("verificaciones");

        jMenuItem2.setText("validar expresion");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem2);

        jMenuBar2.add(jMenu4);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(133, 133, 133)
                                .addComponent(jBConvertir))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jBvalidar)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel3)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField2)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE))))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBvalidar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBConvertir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMLerAutomataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMLerAutomataActionPerformed
        CargarXml cargarXml = new CargarXml(this, true,a);
        cargarXml.setLocationRelativeTo(null);
        cargarXml.setVisible(true);
    }//GEN-LAST:event_jMLerAutomataActionPerformed

    private void jBConvertirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBConvertirActionPerformed

         Automata automata_determinista= a.convertirAFNaAFD(a);
        cargarAutomata(automata_determinista,(DefaultTableModel)JDeterminista.getModel());
     JDeterminista.setVisible(true);
    }//GEN-LAST:event_jBConvertirActionPerformed

    private void jBvalidarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBvalidarActionPerformed
     String cadena= jTextField1.getText();
     //agregamos el inicio y final de la expresion
     String nuevacadena="^";
     nuevacadena=nuevacadena+cadena;
     nuevacadena=nuevacadena+"+$";
     String expresion_a_validar=jTextField2.getText();
    if(ExpresionRegular.expersionIsGenerada( expresion_a_validar,nuevacadena)) {
    jTextField3.setText("si se genera");
    }else{
    jTextField3.setText("no se genera");
    }
    }//GEN-LAST:event_jBvalidarActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        jTextField1.setVisible(true);
        jTextField2.setVisible(true);
        jTextField3.setVisible(true);
        jLabel1.setText("Ingrese expresion validadora : ");
        jLabel2.setText("Ingrese expresion a validar :");
        jLabel3.setText("resultado:");
        jBvalidar.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    public void cargarAutomata(Automata automata, DefaultTableModel dtm) {
//        DefaultTableModel model = (DefaultTableModel) jTNoDeterminista.getModel();
      //  LeerXml lx = new LeerXml();
        if (automata != null) {
            nombrarColumnas(dtm, automata);
            dtm.setRowCount(automata.getConjunto_estados().size());
            dtm.setColumnCount(automata.getConjunto_alfabeto().size() + 1);
            llenarColumnaEstados(dtm, automata);
            llenarTablaconTrasiciones(dtm, automata);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void nombrarColumnas(DefaultTableModel model, Automata a) {
        List<String> cadena = a.getConjunto_alfabeto();

        List<String>prueba = new ArrayList<>();
        for (int i = 0; i <=cadena.size(); i++) {
            if (i == 0) {
                prueba.add(0,"Estados" );
            } else {
                prueba.add(cadena.get(i-1));
                }
        }
        String ob[]=new String[prueba.size()];
        for(int i=0;i<prueba.size();i++){
        ob[i]=prueba.get(i);
        }
        model.setColumnIdentifiers(ob);
    }

    public static void llenarColumnaEstados(DefaultTableModel model, Automata a) {
        List<Integer> v = a.getConjunto_estados();
        Object ob[]=new Object[v.size()];
        for(int i=0;i<v.size();i++){
        ob[i]=v.get(i);
        }
        for (int i = 0; i < v.size(); i++) {
            model.setValueAt(ob[i], i, 0);
        }

    }

    public static void llenarTablaconTrasiciones(DefaultTableModel model, Automata a) {
        String buscafila = "";
        String buscacolumna = "";
        String elemento_a_guardar = "";
        String newdato = "";
        int posicionbuscafila = 0;
        int posicionbuscacolumna = 0;
        List<String>v=a.getConjunto_trasiciones();
         for (int i = 0; i < a.getConjunto_trasiciones().size(); i++) {
            String TransicionN = v.get(i);
            for (int j = 0; j < TransicionN.length(); j++) {
                if (TransicionN.charAt(j) == ',') {
                    buscacolumna = "" + TransicionN.charAt(j + 1);
                    for (int k = (j + 3); k < TransicionN.length(); k++) {
                        elemento_a_guardar = " " + elemento_a_guardar + TransicionN.charAt(k);
                    }
                    break;
                }
                buscafila = buscafila + TransicionN.charAt(j);
            }
            int m = 0;
            for (int l = 0; l < (a.getConjunto_estados().size() + 1); l++) {
                String prueba = "" + model.getValueAt(l, m);
                if (prueba.equals(buscafila)) {
                    posicionbuscafila = l;
                    break;
                }
            }
            for (int n = 1; n < (a.getConjunto_alfabeto().size() + 1); n++) {
                String prueba = model.getColumnName(n);
                if (prueba.equals(buscacolumna)) {
                    posicionbuscacolumna = n;
                    break;
                }
            }
            newdato = (String) model.getValueAt(posicionbuscafila, posicionbuscacolumna);
            if (newdato != null) {
                elemento_a_guardar = newdato + elemento_a_guardar;
            }
            model.setValueAt(elemento_a_guardar, posicionbuscafila, posicionbuscacolumna);
            buscacolumna = "";
            buscafila = "";
            elemento_a_guardar = "";
        }
    }

    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
       
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewInicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ViewInicio().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable JDeterminista;
    private javax.swing.JButton jBConvertir;
    private javax.swing.JButton jBvalidar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuItem jMLerAutomata;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTNoDeterminista;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
